package com.jr.controller;

import com.google.gson.Gson;
import com.jr.pojo.WorkOrder;
import com.jr.service.IWorkOrderService;
import com.jr.service.impl.WorkOrderServiceImpl;
import com.jr.util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

@WebServlet("/wo/*")
public class WorkOrderController extends HttpServlet {
    private IWorkOrderService iwos = new WorkOrderServiceImpl();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求的URI，例如：/asd/projectInfo/queryByPid
        String requestURI = request.getRequestURI();

        // 找到最后一个"/"的位置并加1，得到方法名的起始位置
        int index = requestURI.lastIndexOf("/") + 1;

        // 从URI中提取方法名，例如：queryByPid
        String mName = requestURI.substring(index);


        // 获取当前类的Class对象
        Class cla = this.getClass();
        Method method = null;
        try {
            // 通过反射获取指定方法名的Method对象
            // 该方法需要接受HttpServletRequest和HttpServletResponse两个参数
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            response.setContentType("application/json;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            // 使用反射调用获取到的方法，传入当前对象和请求、响应参数
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            // 如果找不到对应的方法，打印异常堆栈
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // 如果方法不可访问，打印异常堆栈
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // 如果方法调用抛出异常，打印异常堆栈
            e.printStackTrace();
        }
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
  /*      "executor":executor,
                "description":description,
                "orderlevel":orderlevel,
                "pid":pid*/
        String executor = request.getParameter("executor");
        String description = request.getParameter("description");
        Integer orderlevel = Integer.parseInt(request.getParameter("orderlevel"));

        Integer pid = Integer.parseInt(request.getParameter("pid"));

        /*Integer pid, String executor, String description, Integer orderlevel, Date createDate*/
        WorkOrder wo = new WorkOrder(pid,executor,description,orderlevel,new Date());//new Date是动态的
        boolean b = iwos.addWorkOrder(wo);
        response.getWriter().print(new Gson().toJson(b));


    }
    public void getListByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PageHelper ph = new PageHelper();//ph是对象
        int indexpage  =Integer.parseInt(request.getParameter("indexpage"));//indexpage是wolist.jsp给controller的，根据indexpage获取，name为indexpage,value就是=前面的那个
        ph.setPageIndex(indexpage);
        ph.setDateCount(iwos.queryPageDateCount());
        ph.setListIndex(iwos.queryIndexDate(ph));
/*{
  "listIndex": [...],
  "pageIndex": 1,
  "pageCount": 5
}*/
        response.getWriter().println(new Gson().toJson(ph));
    }
}
